if (typeof($settings) == "undefined")
{
    $settings = null;
    chrome.extension.sendRequest({ action: "getSettings" }, function(response) {
        $settings = response;
    });
}


chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse)
    {
        if (request.action == "settingsChanged")
        {
            $settings = request.newSettings;
        }
    }
);


if (document != null && window != null && window.navigator != null && window.navigator.appVersion.indexOf("Mac") == -1)
{
    if (typeof(magiccube) == "undefined")
    {
        magiccube = {};
    }
    magiccube.Scroll2Navigate = function()
    {
        var me = this;
        
        
        me.scrollProgress = null;
        me.direction = null;
        
        
        me.delayTime = 300;
                
        var _timer = null;
        
        
        var _imgCursor = null; 
        
        me.onmousewheel = function(e)
        {
            if (_timer != null)
            {
                clearTimeout(_timer);
                _timer = null;
            }
            
            if (!me.checkEnabled(e))
            {
                return;
            }
            
            if (me.direction === null)
            {
                me.scrollProgress = 1;
                if (e.wheelDeltaX > 0)
                {
                    // Go back
                    me.direction = -1;
                }
                else
                {
                    // Go forward
                    me.direction = 1;
                }
                
                if (_imgCursor == null)
                {
                    _imgCursor = document.createElement("img");
                    _imgCursor.style.position = "absolute";
                    _imgCursor.style.zIndex = 100;
                }
                
                var action = null;
                if (me.direction == 1)
                {
                    action = "forward";
                }
                else if (me.direction == -1)
                {
                    action = "back";                    
                }
                _imgCursor.src = chrome.extension.getURL("images/" + action + ".png");
                
                
                if (document.body != null)
                {
                    document.body.appendChild(_imgCursor);
                    _imgCursor.style.pixelTop = document.body.scrollTop + 120;
                    
                    if (me.direction == 1)
                    {
                        _imgCursor.style.pixelLeft = 0;
                    }
                    else if (me.direction == -1)
                    {
                        _imgCursor.style.pixelLeft = document.body.offsetWidth - 160;
                    }
                }
            }
            else
            {
                if (me.direction == -1)
                {
                    me.scrollProgress  += e.wheelDeltaX / $settings.sensitivity;
                }
                else if (me.direction == 1)
                {
                    me.scrollProgress -= e.wheelDeltaX / $settings.sensitivity;
                }
            }
            
            if (me.scrollProgress > $settings.effectiveRange)
            {
                me.scrollProgress = 100;
            }
            else if (me.scrollProgress < 0)
            {
                me.scrollProgress = 0;
            }
            
            if (me.scrollProgress == 100)
            {
                me.ontimeout();
            }
            else
            {
                _timer = setTimeout(me.ontimeout, me.delayTime);                
            }
            
            if (document.body != null)
            {
                var rate = me.scrollProgress / 100;
                if (me.direction == 1)
                {
                    _imgCursor.style.pixelLeft = rate * (document.body.offsetWidth - 160);
                }
                else if (me.direction == -1)
                {
                    _imgCursor.style.pixelLeft = (1 - rate) * (document.body.offsetWidth - 160);
                }
                _imgCursor.style.opacity = rate;
            }
        };
        
        
        
        me.ontimeout = function()
        {
            if (me.scrollProgress == 100)
            {
                me.navigate();
            }
            else
            {
                me.cancel();                
            }
        };
        
        
        
        
        me.navigate = function()
        {
            _imgCursor.style.webkitTransition = "all 0.3s ease-in-out";
            if (me.direction == 1)
            {
                _imgCursor.style.pixelLeft = document.body.offsetWidth - 160;
            }
            else if (me.direction == -1)
            {
                _imgCursor.style.pixelLeft = 0;
            }
            _imgCursor.style.opacity = 0;
            
            setTimeout(function()
            {
                if (me.direction == 1)
                {
                    window.history.forward();
                }
                else if (me.direction == -1)
                {
                    window.history.back();
                }
                me.direction = null;
                me.scrollProgress = null;
                
                document.body.removeChild(_imgCursor);
                _imgCursor.style.webkitTransition = "";
            }, 300);
        };
        
        me.cancel = function()
        {
            _imgCursor.style.webkitTransition = "all 0.3s ease-in-out";
            if (me.direction == 1)
            {
                _imgCursor.style.pixelLeft = 0;
            }
            else if (me.direction == -1)
            {
                _imgCursor.style.pixelLeft = document.body.offsetWidth - 160;
            }
            _imgCursor.style.opacity = 0;
            
            me.direction = null;
            me.scrollProgress = null;
            
            
            setTimeout(function()
            {
                document.body.removeChild(_imgCursor);
                _imgCursor.style.webkitTransition = "";
            }, 300);
        };
        
        
        
        
        me.checkEnabled = function(e)
        {
            if (e.altKey || e.ctrlKey || e.shiftKey || !$settings.enabled || e.wheelDeltaX == 0 || document == null)
            {
                return false;
            }
            
            if (document.body != null && document.body.scrollWidth > document.body.clientWidth)
            {
                return false;
            }
            
            return true;
        };
        
        return me;
    };





    //===================================================================
    var s2n = new magiccube.Scroll2Navigate();
    document.addEventListener("mousewheel", s2n.onmousewheel);
}